<?php
/**
 * Task Storage facade
 * @author Kirill A Egorov 2011
 */
class Bgtask_Manager
{
	
	const LAUNCHER_HTTP = 'Bgtask_Launcher_Local_Http';
	const LAUNCHER_JSON = 'Bgtask_Launcher_Local_Json';
	
	const STORAGE_ORM = 'Bgtask_Storage_Orm';
	
    static protected $_instance = null;
    /**
     * @return Bgtask_Manager
     */
    static public function getInstance(){
        if(is_null(self::$_instance))
            self::$_instance = new self;
        return  self::$_instance;    
    }
    
    protected function __construct(){}
    protected function __clone(){}
    
    /**
     * Task Storage
     * @var Bgtask_Storage
     */
    protected $_storage = null;
    /**
     * Task activity logger
     * @var Bgtask_Log
     */
    protected $_logger = null;
    /**
     * Define task storage
     * @param Bgtask_Manager_Storage $storage
     */
    public function setStorage($storage){
        $this->_storage = $storage;
    }   
    /**
     * Set task logger
     * @param Bgtask_Log $logger
     */
    public function setLogger(Bgtask_Log $logger){
    	$this->_logger = $logger;
    }
    /**
     * Get task activity logger
     */
    public function getLogger(){
    	return $this->_logger;
    }
    /**
     * Launch background task
     * Enter description here ...
     * @param string $launcher - const, task launcher
     * @param string $task - task object class name
     * @param array $config
     */
    public function launch($launcher , $task , array $config){
        if(!class_exists($launcher))
            trigger_error('Invalid Task launcher '.$launcher);
            
        $launcher = new $launcher;
        $launcher->launch($task , $config);
    }
    /**
     * Get current tasks list
     * @return array
     */
    public function getList(){
        return $this->_storage->getList();
    }
    /**
     * Get Task info by Id
     * @param integer $pid
     * @return array
     */
    public function get($pid){
        return $this->_storage->get($pid);
    }    
     /**
     * Send signal
     * @param integer $pid
     * @param integer $signal - const
     * @return void
     */
    public function signal($pid , $signal){
        return $this->_storage->signal($pid, $signal);
    }
    /**
     * Set task config
     * @param integer $pid
     * @param array $config
     * @return void
     */
    public function setConfig($pid , array $config){
        return $this->_storage->setConfig($pid, $config);
    }
    /**
     * Get task config
     * @param integer $pid
     * @return array
     */
    public function getConfig($pid){
        return $this->_storage->getConfig($pid);
    }
    /**
     * Kill task
     * @param integer $pid
     * @return void
     */
     public function kill($pid){
         return $this->_storage->kill($pid);
     }
      /**
     * Get task signals
     * @param integer $pid
     * @param boolean $clean - remove signals after reading
     * @return array();
     */
    public function getSignals($pid , $clean = false){
        return $this->_storage->getSignals($pid , $clean);
    }
    /**
     * Remove signals
     * @param integer $pid
     * @param array $sigId - optional
     * @return array
     */
    public function clearSignals($pid , $sigId = false){
        return $this->_storage->clearSignals($pid , $sigId);
    }
    /**
     * Update task state
     * @param integer $pid
     * @param integer $opFinished 
     * @param integer $opFinished 
     * @param integer $status - status constant
     */
    public function updateState($pid , $opTotal , $opFinished , $status){
    	$memoryPeak = memory_get_peak_usage();
    	$memoryAllocated = memory_get_usage();
    	$this->_storage->updateState($pid , $opTotal , $opFinished , $status, $memoryPeak , $memoryAllocated);
    }
    /** 
     * Check task record
     * @param integer $pid
     * @return boolean
     */
    public function isLive($pid){
    	return $this->_storage->isLive($pid);
    }
    /**
     * Set task started
     * @param integer $pid
     */
    public function setFinished($pid){
    	return $this->_storage->setFinished($pid, date('Y-m-d H:i:s'));
    }
    /**
     * Stop task execution
     * @param integer $pid
     */
    public function setStoped($pid){
    	return $this->_storage->setStoped($pid, date('Y-m-d H:i:s'));
    }
    /**
     * Set task finished
     * @param integer $pid
     */
    public function setStarted($pid){
    	return $this->_storage->setStarted($pid, date('Y-m-d H:i:s'));
    }
    /**
     * Add task record , and get it`s pid
     * @param string $description
     * @return integer
     */
    public function addTaskRecord($description){
    	return $this->_storage->addTaskRecord($description);
    }
}